package com.example.drainage.ui.calendar

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.drainage.R
import java.util.*

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.drainage.ui.calendar
 * @createTime 创建时间 ：2021/11/25
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class CalendarMonthAdapter : BaseQuickAdapter<CalendarBean, BaseViewHolder>(R.layout.item_scheduling_calendar_day) {

    private val notShowDay = -1
    private val year: Int
    private val month: Int
    private val day: Int

    private var selectYear: Int
    private var selectMonth: Int
    private var selectDay: Int

    var selectListener: (() -> Unit)? = null

    private val bgDrawable = GradientDrawable().apply {
        shape = GradientDrawable.OVAL
        setColor(Color.parseColor("#FF1F81F2"))
    }

    private val bottomDrawable = GradientDrawable().apply {
        shape = GradientDrawable.OVAL
        setColor(Color.parseColor("#FFF9A806"))
    }

    init {
        val calendar = Calendar.getInstance()
        year = calendar.get(Calendar.YEAR)
        month = calendar.get(Calendar.MONTH) + 1
        day = calendar.get(Calendar.DATE)

        val selectCalendar = SchedulingCalendarUtil.getSelectCalendar()
        selectYear = selectCalendar.get(Calendar.YEAR)
        selectMonth = selectCalendar.get(Calendar.MONTH) + 1
        selectDay = selectCalendar.get(Calendar.DATE)
    }

    override fun convert(holder: BaseViewHolder, item: CalendarBean) {
        val contentView = holder.getView<View>(R.id.day_content)
        val pointView = holder.getView<View>(R.id.tv_point)
        if (item.day == notShowDay) {
            holder.setText(R.id.tv_day, null)
            contentView.setOnClickListener(null)
        } else if (year == item.year && month == item.month && day == item.day) {
            holder.setText(R.id.tv_day, "今天")
            contentView.setOnClickListener {
                SchedulingCalendarUtil.updateSelectCalendar(item.year, item.month - 1, item.day)
                selectListener?.invoke()
            }
        } else {
            holder.setText(R.id.tv_day, item.day.toString())
            contentView.setOnClickListener {
                SchedulingCalendarUtil.updateSelectCalendar(item.year, item.month - 1, item.day)
                selectListener?.invoke()
            }
        }
        if (selectYear == item.year && selectMonth == item.month && selectDay == item.day) {
            contentView.background = bgDrawable
            holder.setTextColor(R.id.tv_day, Color.WHITE)
        } else {
            contentView.background = null
            holder.setTextColor(R.id.tv_day, Color.parseColor("#FF333333"))
        }
        if (item.day % 4 == 0) {
            pointView.visibility =View.VISIBLE
            pointView.background = bottomDrawable
        } else {
            pointView.visibility =View.INVISIBLE
        }
    }

    override fun setNewInstance(list: MutableList<CalendarBean>?) {
        super.setNewInstance(list)
        val selectCalendar = SchedulingCalendarUtil.getSelectCalendar()
        selectYear = selectCalendar.get(Calendar.YEAR)
        selectMonth = selectCalendar.get(Calendar.MONTH) + 1
        selectDay = selectCalendar.get(Calendar.DATE)
    }
}